﻿using System;
using System.Configuration;

namespace MyTime.Core
{
    /// <summary>
    /// Will read and write with MyTime.app.config
    /// </summary>
    public static class ConfigHelper
    {
        private static Configuration _conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        /// <summary>
        /// Gets the value from appConfig
        /// </summary>
        /// <param name="key">The key of the appConfig section</param>
        /// <returns>a string value</returns>
        public static string Get(string key)
        {
            var obj = _conf.AppSettings.Settings[key];
            if (obj == null)
                return null;
            if (string.IsNullOrEmpty(obj.Value))
                return null;
            return obj.Value;
        }
        /// <summary>
        /// Will set a value in a appConfig section
        /// </summary>
        /// <param name="key">The key to set</param>
        /// <param name="value">The value to apply to the key</param>
        public static void Set(string key, string value)
        {
            _conf.AppSettings.Settings[key].Value = value;
            _conf.Save();
        }
    }
}
